package com.suisung.shopsuite.account.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.suisung.shopsuite.account.model.entity.UserInfo;
import com.suisung.shopsuite.account.model.entity.UserProxy;
import com.suisung.shopsuite.account.model.req.UserProxyReq;
import com.suisung.shopsuite.account.service.UserInfoService;
import com.suisung.shopsuite.account.service.UserProxyService;
import com.suisung.shopsuite.common.utils.ContextUtil;
import com.suisung.shopsuite.core.web.BaseQueryWrapper;
import com.suisung.shopsuite.core.web.CommonRes;
import com.suisung.shopsuite.core.web.controller.BaseController;
import com.suisung.shopsuite.core.web.model.res.BaseListRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Api(tags = "用户申请代理信息表")
@RestController
@RequestMapping("/manage/account/userProxy")
public class UserProxyController extends BaseController {

    @Resource
    private UserProxyService userProxyService;

    @Resource
    private UserInfoService userInfoService;

    @PreAuthorize("hasAuthority('/manage/account/userProxy/list')")
    @ApiOperation(value = "用户申请代理信息表-分页列表查询", notes = "用户申请代理信息表-分页列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonRes<BaseListRes<UserProxy>> list(UserProxyReq userProxyReq) {
        QueryWrapper<UserProxy> wrapper = new BaseQueryWrapper<UserProxy, UserProxyReq>(userProxyReq).getWrapper();

        if (!StringUtils.isEmpty(userProxyReq.getContacts())){
            wrapper.like("contacts",userProxyReq.getContacts());
        }

        if (!StringUtils.isEmpty(userProxyReq.getTelephone())){
            wrapper.like("telephone",userProxyReq.getTelephone());
        }

        IPage<UserProxy> pageList = userProxyService.lists(new BaseQueryWrapper<UserProxy, UserProxyReq>(userProxyReq).getWrapper(), userProxyReq.getPage(), userProxyReq.getSize());
        pageList.getRecords().forEach(bean -> {
            UserInfo userInfo = userInfoService.get(bean.getUserId());
            bean.setUserInfo(userInfo);
        });
        return success(pageList);
    }

    @ApiOperation(value = "用户申请代理信息表-根据id查询详情", notes = "用户申请代理信息表-根据id查询详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonRes<?> get(@PathVariable Integer id) {

        try {
            UserProxy userProxy = userProxyService.get(id);

            if (userProxy != null) {
                UserInfo userInfo = userInfoService.get(userProxy.getUserId());
                userProxy.setUserInfo(userInfo);
            }

            return success(userProxy);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return fail();
    }

    @PreAuthorize("hasAuthority('/manage/account/userProxy/edit')")
    @ApiOperation(value = "用户申请代理信息表-编辑", notes = "用户申请代理信息表-编辑")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public CommonRes<?> edit(UserProxyReq userProxyReq) {
        UserProxy userProxy = BeanUtil.copyProperties(userProxyReq, UserProxy.class);
        userProxy.setUpdateTime(new DateTime());

        /*申请通过之后处理当前用户的代理权限*/
//        if (userProxy.getStatus() != null && userProxy.getStatus() == 1) {
            if (userProxyReq.getUserId() != null) {
                UserInfo userInfo = userInfoService.get(userProxyReq.getUserId());
                if (userInfo != null) {
                    userInfo.setUserParentId(userProxyReq.getUserParentId());
                    userInfo.setUserLevelId(userProxyReq.getUserLevelId());
                    userInfoService.editUser(userInfo);

                    boolean success = userProxyService.edit(userProxy);

                    if (success) {
                        return success();
                    }
                } else {
                    return fail("无法获取当前申请用户信息");
                }
            } else {
                return fail("无法获取当前申请用户信息");
            }
        //}

        boolean success = userProxyService.edit(userProxy);

        if (success) {
            return success();
        }

        return fail();
    }

    @ApiOperation(value = "用户申请代理信息表-新增", notes = "用户申请代理信息表-新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonRes<?> add(UserProxyReq userProxyReq) {
        UserProxy userProxy = BeanUtil.copyProperties(userProxyReq, UserProxy.class);
        userProxy.setCreateTime(new DateTime());
        Integer userId = ContextUtil.checkLoginUserId();
        userProxy.setUserId(userId);
        boolean success = userProxyService.add(userProxy);

        if (success) {
            return success();
        }

        return fail();
    }

    @PreAuthorize("hasAuthority('/manage/account/userProxy/remove')")
    @ApiOperation(value = "用户申请代理信息表-通过id删除", notes = "用户申请代理信息表-通过id删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public CommonRes<?> remove(@RequestParam("id") Integer userId) {
        boolean success = userProxyService.remove(userId);

        if (success) {
            return success();
        }

        return fail();
    }

}
